package com.v1.video.view;

import com.v1.video.util.Logger;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.view.View;

/**
 * Created by xbc on 16/3/2.
 * @author xinbaicheng@kingsoft.com
 */
public class VideoSurfaceView extends SurfaceView {
	public static final String TAG = "VideoPlayerActivityJin";
    private int mVideoWidth;
    private int mVideoHeight;

    private int mMeasuredWidth;
    private int mMeasuredHeight;

    public VideoSurfaceView(Context context) {
        super(context);
    }

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VideoSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        doMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mMeasuredWidth, mMeasuredHeight);
    }

    public void setVideoDimension(int width, int height) {
        mVideoWidth = width;
        mVideoHeight = height;
    }
    
    private void doMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        mMeasuredWidth = mVideoWidth;
        mMeasuredHeight = mVideoHeight;
    }

//    private void doMeasure(int widthMeasureSpec, int heightMeasureSpec)
//    {
//    	Logger.i(TAG, "mVideoWidth2222222222==="+mVideoWidth+"_mVideoHeight=="+mVideoHeight);
//    	Logger.i(TAG, "widthMeasureSpech2222222222==="+widthMeasureSpec+"_heightMeasureSpec=="+heightMeasureSpec);
//        int width = View.getDefaultSize(mVideoWidth, widthMeasureSpec);
//        int height = View.getDefaultSize(mVideoHeight, heightMeasureSpec);
//        Logger.i(TAG, "width222==="+width+"_height=="+height);
//        if(mVideoWidth > 0 && mVideoHeight > 0)
//        {
//            int widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec);
//            int heightSpecSize = View.MeasureSpec.getSize(heightMeasureSpec);
//            float specAspectRatio = (float) widthSpecSize / (float) heightSpecSize;
//            float displayAspectRatio = (float) mVideoWidth / (float) mVideoHeight;
//            boolean shouldBeWider = displayAspectRatio > specAspectRatio;
//
//            if (shouldBeWider) {
//                // not high enough, fix height
//                height = heightSpecSize;
//                width = (int) (height * displayAspectRatio);
//                Logger.i(TAG, "shouldBeWider=="+true);
//            } else {
//                // not wide enough, fix width
//                width = widthSpecSize;
//                height = (int) (width / displayAspectRatio);
//                Logger.i(TAG, "shouldBeWider=="+false);
//            }
//        }
//
//        mMeasuredWidth = width;
//        mMeasuredHeight = height;
//        Logger.i(TAG, "mMeasuredWidth2222222222==="+mMeasuredWidth+"_mMeasuredHeight=="+mMeasuredHeight);
//    }
}
